﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iuPlace.Core.Models;

namespace iuPlace.Core.Cache
{
    public class UserCache
    {

        #region Static
        static object LockUsersOnline = new object();

        private static Dictionary<Int64, UserOnline> _UsersOnline = new Dictionary<Int64, UserOnline>();
        public static IEnumerable<UserOnline> UsersOnline
        {
            get
            {
                return _UsersOnline.Values;
            }
        }

        public static void AddUserOnline(UserOnline userOnline)
        {
            lock (UserCache.LockUsersOnline)
            {
                if (!UserCache._UsersOnline.ContainsKey(userOnline.User.Id))
                    UserCache._UsersOnline.Add(userOnline.User.Id, userOnline);
            }
        }

        public static void RemoveUserOnline(Int64 id)
        {
            lock (UserCache.LockUsersOnline)
            {
                if (UserCache._UsersOnline.ContainsKey(id))
                {
                    var userOnLine = UserCache._UsersOnline[id];                  
                    userOnLine.Dispose();
                    UserCache._UsersOnline.Remove(id);
                }
            }
        }

        public static UserOnline GetUserOnline(Int64 id)
        {
            if (UserCache._UsersOnline.ContainsKey(id))
                return UserCache._UsersOnline[id];
            else
                return null;
        }
        #endregion
    }
}
